package com.df.bpm.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.df.bpm.dto.BusinessLocationsDTO4Subset;
import com.df.bpm.entity.BusinessLocations;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface BusinessLocationsMapper extends BaseMapper<BusinessLocations> {

    @Select("SELECT * \n" +
            "FROM businessOverview.businessLocations \n" +
            "where groupID=#{groupID} and superior=#{superior} \n" +
            "ORDER BY groupID,sequence")
    List<BusinessLocations> getSteps(Integer groupID, Integer superior);

    @Select("SELECT groupID \n" +
            "FROM businessOverview.businessLocations \n" +
            "where superior=#{superior} \n" +
            "GROUP BY groupID \n" +
            "ORDER BY groupID")
    List<Integer> getGroups(Integer superior);

    @Update("update businessOverview.businessLocations \n" +
            "set sequence=sequence+#{length} \n" +
            "where superior=#{superior} \n" +
            "and groupID=#{groupID} \n" +
            "and sequence>=#{headSequence} \n" +
            "and sequence<=#{tailSequence}")
    void updateSequences(int length, Integer headSequence, Integer tailSequence, Integer superior, Integer groupID);

    @Update("update businessOverview.businessLocations \n" +
            "set sequence=sequence-1 \n" +
            "where superior=#{superior} \n" +
            "and groupID=#{groupID} \n" +
            "and sequence>=#{sequence} ")
    void updateSequences4delete(Integer sequence, Integer superior, Integer groupID);

    /**
     * 查询目标业务目标分组子级流程列表
     * @param superior  目标业务id
     * @param groupId   目标分组id
     * @return  子集业务流程
     */
    @Select("SELECT * \n" +
            "FROM businessOverview.businessLocationsDTO4Subset \n" +
            "where superior=#{superior} and groupID=#{groupId} \n" +
            "ORDER BY superior,groupID,sequence  ")
    List<BusinessLocationsDTO4Subset> getStepDto(Integer superior, Integer groupId);
}